<?php 

class TranslationModel extends BaseModel{
	
	private $table = null;
	private $systemModel = null;
	
	public function __construct(){
		
		$this->table = TABLE_PREFIX . "translations";
		$this->systemModel = SystemModel::getInstance();
	}
	
	public function getTranslations($id_language){
		
		return dibi::select("*")
					->from($this->table)
					->where("id_language = %i", $id_language)
					->fetchAll();
	}
	
	public function getTranslationByKey($key, $id_language){
		
		return dibi::select("*")
					->from($this->table)
					->where("key = %s", $key)
					->and("id_language = %i", $id_language)
					->fetch();
	}
	
	public function addTranslation($key, $value = ""){
		
		$languages = $this->systemModel->getLanguages();
		
		foreach($languages as $l){
			$trans = array(
						"id_language" => $l["id_language"],
						"key" => $key,
						"translation" => $value
						);
			
			$exists = dibi::select("key")
						->from($this->table)
						->where("`key` = %s", $key)
						->and("id_language = %i", $l["id_language"])
						->fetch();
						
			if(empty($exists)) dibi::insert($this->table, $trans)
									->execute();
									 
		}
	}
	
	public function editTranslation($key, $value, $id_language){
		
		$trans = array(
						"translation" => $value
						);
		
		return dibi::update($this->table, $trans)
					->where("`key` = %s", $key)
					->and("id_language = %i", $id_language)
					->execute();
	}
	
}

?>